﻿#region License
/*Copyright (c) 2010 Andrew Collard (andrewcollard@hotmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#endregion

using System.IO;
using System;

namespace TorchView.Game
{
  /// <summary>
  /// An item in Torchlight
  /// </summary>
  public sealed class Item
  {
    const string ITEM_NAME_TAG = "[ITEM]";

    public long ItemModelID { get; set; }  //Class/parent/base item uuid. Aka Model nbr.
    public string Name { get; set; }
    public string Prefix { get; set; }
    public string Suffix { get; set; }
    public long ItemID { get; set; }    //uuid for this specific instance of an item. Aka Serial nbr.
    public long Unknown1 { get; set; }
    public byte Unknown2 { get; set; }
    public long Unknown3 { get; set; }
    public long Unknown4 { get; set; }
    public uint TimesHeirloomed { get; set; }
    public uint TimesEnchanted { get; set; }
    public ItemSlotType InventorySlot { get; set; }
    public byte Unknown7 { get; set; }
    public byte Unknown8 { get; set; }
    public byte Unknown9 { get; set; }
    public byte Unknown10 { get; set; }
    public byte Unknown11 { get; set; }
    public bool Identified { get; set; }
    public Vector Position { get; set; }
    public Vector Position2 { get; set; }
    public Matrix Orientation { get; set; }
    public uint Stack { get; set; }
    public int TotalSockets { get; set; }
    public Item[] UsedSockets { get; set; }
    public int MaxDamage { get; set; }
    public int Armor { get; set; }
    public int Unknown12 { get; set; }
    public int Unknown13 { get; set; }
    public int Unknown14 { get; set; }
    public int Unknown15 { get; set; }
    public DamageBonus[] BonusDamage { get; set; }
    public Effect[] PassiveEffects { get; set; }        //Effects always on the player when wearing the item.
    public Effect[] UseEffects { get; set; }            //Effects from using an item, like a potion or fish.
    public Effect[] TargetEffects { get; set; }         //Effects that actually result in a proc, like target flees for x seconds

    public static Item Read(BinaryReader reader)
    {
      Item item = new Item();
      item.ItemModelID = reader.ReadInt64();
      item.Name = SVTHelper.ReadString(reader);
      item.Prefix = SVTHelper.ReadString(reader);
      item.Suffix = SVTHelper.ReadString(reader);
      item.ItemID = reader.ReadInt64();

      item.Unknown1 = reader.ReadInt64();
      item.Unknown2 = reader.ReadByte();
      item.Unknown3 = reader.ReadInt64();
      item.Unknown4 = reader.ReadInt64();
      item.TimesHeirloomed = reader.ReadUInt32();
      item.TimesEnchanted = reader.ReadUInt32();
      item.InventorySlot = (ItemSlotType)reader.ReadUInt32();
      item.Unknown7 = reader.ReadByte();
      item.Unknown8 = reader.ReadByte();
      item.Unknown9 = reader.ReadByte();
      item.Unknown10 = reader.ReadByte();
      item.Unknown11 = reader.ReadByte();
      item.Identified = reader.ReadBoolean();
      item.Position = Vector.Read(reader);
      item.Position2 = Vector.Read(reader);
      item.Orientation = Matrix.Read(reader);
      item.Stack = reader.ReadUInt32();
      item.TotalSockets = reader.ReadInt32();
      item.UsedSockets = ReadUsedSockets(reader);
      item.MaxDamage = reader.ReadInt32();
      item.Armor = reader.ReadInt32();
      item.Unknown12 = reader.ReadInt32();
      item.Unknown13 = reader.ReadInt32();
      item.Unknown14 = reader.ReadInt32();
      item.Unknown15 = reader.ReadInt32();
      item.BonusDamage = ReadBonusDamage(reader);
      item.PassiveEffects = ReadEffects(reader);
      item.UseEffects = ReadEffects(reader);
      item.TargetEffects = ReadEffects(reader);
      return item;
    }

    private static Effect[] ReadEffects(BinaryReader reader)
    {
      var effectCount = reader.ReadUInt32();
      Effect[] effects = new Effect[effectCount];
      for (int i = 0; i < effectCount; i++)
        effects[i] = Effect.Read(reader);
      return effects;
    }

    private static DamageBonus[] ReadBonusDamage(BinaryReader reader)
    {
      var damageBonusCount = reader.ReadUInt16();
      DamageBonus[] bonuses = new DamageBonus[damageBonusCount];
      for (int i = 0; i < damageBonusCount; i++)
        bonuses[i] = DamageBonus.Read(reader);
      return bonuses;
    }

    private static Item[] ReadUsedSockets(BinaryReader reader)
    {
      var usedSockets = reader.ReadUInt32();

      Item[] sockets = new Item[usedSockets];
      for (int i = 0; i < usedSockets; i++)
        sockets[i] = Read(reader);

      return sockets;
    }

    private string GetFullName()
    {
      return string.Concat(Prefix.Replace(ITEM_NAME_TAG, string.Empty), Name, Suffix.Replace(ITEM_NAME_TAG, string.Empty));
    }

    public override string ToString()
    {
      return GetFullName();
    }
  }
}
